package org.samoji.sji.model;
import java.math.BigDecimal;
import java.math.MathContext;

import org.samoji.sji.profTokenizer.Token.Type;

public class DivideBinExpr extends BinExpr {
	public DivideBinExpr(Expr left, Expr right) {
		super(Type.Div, left, right);
	}

	@Override
	public BigDecimal evaluate(Env env) {
		return left.evaluate(env).divide(checkIfZero(right.evaluate(env)), MathContext.DECIMAL128);
	}

	private BigDecimal checkIfZero(BigDecimal value) {
		if(value.compareTo(BigDecimal.ZERO) == 0) {
			throw new RuntimeException("run-time: right operand is ZERO");
		} else {
			return value;
		}
	}
	
	public static DivideBinExpr _(Expr left, Expr right) {
		return new DivideBinExpr(left, right);
	}
}